<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * @Date: 2021-07-05 10:16:09
 * @LastEditors: Sam
 * @LastEditTime: 2022-04-12 16:35:37
 */


namespace app\develop\controller;

// 菜单管理
use think\App;
use think\facade\Db;
use think\Env;
use think\Request;
use Generate;

class Curd extends Base{

	private $modu;
    private $name;
    private $dir;
    private $namespaceSuffix;
    private $nameLower;
    private $data;
	public function index(){
        $dbtables = Db::query("SELECT TABLE_NAME Name FROM information_schema.tables WHERE TABLE_NAME LIKE '%".config('database.connections.mysql.prefix')."_%'");
        $this->assign('tables', $dbtables);
        if(request()->param('table')){
            $table      = request()->param('table');
            $prefix     = app()->env->get(SYSTEM.'db_pre');
            $tableInfo  = Db::table($table)->getFieldsType();
            foreach ($tableInfo as $key => $value) {
                $tableInfo[] = $key;
                unset($tableInfo[$key]);
            }
            $controller = parseName(preg_replace('/^(' . $prefix . ')/', '', $table), 1);
            $this->assign('table_info', json_encode($tableInfo));
            $this->assign('controller', $controller);
        }else{
            $this->assign('table_info', json_encode([]));
        }
        return $this->fetch();
    }



    /**
 * 自动生成提交
 *
 * @return void
 * @Description
 * @example
 * @author Sam
 * @since 
 */
    public function run(){
        $generate = new Generate;
        $data     = request()->post();
         try{
            $result = $generate->run($data, request()->post('file'));
         }catch(\Exception $e){
            json_response(0,$e->getMessage());
         }
       
        json_response(1,'创建成功');
    }
}